Matching
In this part of the assignment, we will implement regular-expression matching, using derivatives. You will do your work in the file src/Evaluation.hs.
To Do
By referring to the equations from the course materials or to the Owens et al. paper, complete the definition of
deriv.Get preliminary confirmation that your code works by running the tests in
test/MatchSpec.hs(which triesregexMatchon a collection of predefined regular expressions and strings, and reports the results). You can run the tests with the command:stack test --test-arguments "--match /Match"Consider adding tests to
test/MatchSpec.hsthat cover the remaining forms ofRegex, making sure that you are testing all the regular-expression operators with both matches and mismatches. You are also welcome to add more regular-expression definitions to the filetest/ExampleRegexes.hs, in addition to the examples there already.
Hints / Comments
The function map may be useful.
The regular expression
Concat [r1, r2, ..., rn]is equivalent toConcat [r1, Concat[r2, ..., rn]]; andConcat []is equivalent toEpsilon.In any DFA that accepts language L, running a string
wthrough the DFA ends in a state whose language is \(∂_w(L)\).The Owens et al. paper writes the derivative function for concatenation using a helper function
νthat either returnsεor∅. For implementation purposes, I recommend using the equivalent but more explicit formula in the course materials, i.e., \(∂_a (r s) = ((∂_a r) s)\) whenris not nullable, and \(∂_a (r s) = ( (∂_a r) s) | (∂_a s) )\) whenris nullable.If you run
stack gchiyou can always try out some expressions for yourself, e.g.,deriv 'a' (Concat [Letter 'a', Letter 'b'])When you write new tests, be very careful that you write down the correct answer! Negation in particular can be tricky: at first glance you might think the regular expression
(¬(ab))*would not matchaborabab, but in fact that pattern matches every string! (Every string can be divided into a finite number of pieces that are notab, e.g., by dividing the string into its individual letters.)